import axios from "axios";
import {message} from "ant-design-vue";
import {getToken} from "@/utils/auth";
import router from "@/router";
import userModule from '@/store/module/user'



const service = axios.create({
    timeout:10000,
    baseURL:process.env.VUE_APP_BASE_URL
})


service.interceptors.request.use((config) => {
    if(getToken() !== '') {
        config.headers.authorization = getToken()
    }
    config.headers["Content-Type"] = config.headers["Content-Type"] ? config.headers["Content-Type"] : 'application/x-www-form-urlencoded'
    return config
},error => {
    return Promise.reject(error)
})

service.interceptors.response.use((res) => {
    let {data:response} = res // 将返回的信息直接拆解
    if(response.code == 400){
        
    }else if(response.code == 401){
        // @ts-ignore
        userModule.mutations.LOGOUT()
        // router.push('/login')
    }
    return response
})


export default service
